package com.bckj.fastboot.iot.data.mysql.repository;

import com.baomidou.mybatisplus.extension.repository.CrudRepository;
import com.bckj.fastboot.core.lang.model.PageResult;
import com.bckj.fastboot.core.page.PageSupport;
import com.bckj.fastboot.iot.common.dto.query.IotAlarmDataQuery;
import com.bckj.fastboot.iot.common.dto.request.IotAlarmDataRequest;
import com.bckj.fastboot.iot.common.dto.response.IotAlarmDataResponse;
import com.bckj.fastboot.iot.common.repository.IotAlarmDataRepository;
import com.bckj.fastboot.iot.data.mysql.convert.IotAlarmDataConvert;
import com.bckj.fastboot.iot.data.mysql.entity.IotAlarmData;
import com.bckj.fastboot.iot.data.mysql.mapper.IotAlarmDataMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * IotAlarmDataRepositoryImpl
 *
 * @author wfc
 **/
@Repository
@RequiredArgsConstructor
public class IotAlarmDataRepositoryImpl extends CrudRepository<IotAlarmDataMapper, IotAlarmData>
implements IotAlarmDataRepository {

    private final IotAlarmDataConvert iotAlarmDataConvert;

    @Override
    public boolean save(IotAlarmDataRequest request) {
        IotAlarmData entity = iotAlarmDataConvert.toEntity(request);
        return save(entity);
    }

    @Override
    public boolean updateById(IotAlarmDataRequest request) {
        IotAlarmData entity = iotAlarmDataConvert.toEntity(request);
        return updateById(entity);
    }

    @Override
    public boolean removeById(Long id) {
        return super.removeById(id);
    }

    @Override
    public IotAlarmDataResponse details(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public IotAlarmDataResponse findLatestOne(IotAlarmDataQuery query) {
        return baseMapper.findLatestOne(query);
    }

    @Override
    public List<IotAlarmDataResponse> findList(IotAlarmDataQuery query) {
        return baseMapper.findList(query);
    }

    @Override
    public PageResult<IotAlarmDataResponse> page(Pageable page, IotAlarmDataQuery query) {
        var result = PageSupport.page(page, query, this::findList);
        return PageResult.of(result);
    }
}
